/********************************************************************************
* File Name:	searchhouse.cpp
* Description:	第7章实例训练
* Reference book:《Linux环境下Qt4图形界面与MySQL编程》，机械工业出版社.2012.1
* E_mail: openlinux2011@gmail.com
*
********************************************************************************/
#include "searchhouse.h"
#include "ui_searchhouse.h"
#include "houserenter.h"
#include <QtSql>
#include <QSqlQuery>
#include <QMessageBox>
#include "databaseconnection.h"

searchhouse::searchhouse(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::searchhouse)
{
    ui->setupUi(this);
}

searchhouse::~searchhouse()
{
    delete ui;
}

void searchhouse::on_btnNotFull_clicked()
{
    if(!databaseconnection::Open())
    {
        QMessageBox::about(this,"Message","connect error");
        return;
    }
    else
    {
        QSqlQueryModel *model = new QSqlQueryModel;
        model->setQuery("select * from house.roomInfo where ratingNum > realNum");
        model->setHeaderData(0,Qt::Horizontal,tr("roomId"));
        model->setHeaderData(1,Qt::Horizontal,tr("renterid"));
        model->setHeaderData(2,Qt::Horizontal,tr("roomtype"));
        model->setHeaderData(3,Qt::Horizontal,tr("location"));
        model->setHeaderData(4,Qt::Horizontal,tr("floor"));
        model->setHeaderData(5,Qt::Horizontal,tr("ratingNum"));
        model->setHeaderData(6,Qt::Horizontal,tr("realNum"));
        model->setHeaderData(7,Qt::Horizontal,tr("area"));
        model->setHeaderData(8,Qt::Horizontal,tr("price"));
        model->setHeaderData(9,Qt::Horizontal,tr("air"));
        model->setHeaderData(10,Qt::Horizontal,tr("phone"));
        model->setHeaderData(11,Qt::Horizontal,tr("tv"));
        model->setHeaderData(12,Qt::Horizontal,tr("washer"));
        model->setHeaderData(13,Qt::Horizontal,tr("internet"));
        model->setHeaderData(14,Qt::Horizontal,tr("fridge"));
        model->setHeaderData(15,Qt::Horizontal,tr("remark"));
        ui->tbvSearchHouse->setModel(model);
        ui->tbvSearchHouse->show();
    }
    databaseconnection::Close();
}


void searchhouse::on_btnLive_clicked()
{
    houserenter *hr = new houserenter();
    hr->show();
}



void searchhouse::on_btnSerach_clicked()
{
    QString floor = ui->lefloor->text().trimmed();
    QString type = ui->letype->text().trimmed();
    QString areamin = ui->leareamin->text().trimmed();
    QString areamax = ui->leareamax->text().trimmed();
    QString pricemin = ui->lepricemin->text().trimmed();
    QString pricemax = ui->lepricemax->text().trimmed();
    QString location = ui->lelocation->text().trimmed();
    QString cmd = "select * from house.roomInfo where ";
    QString cmdTmp = cmd;
    bool flg=false;
    if(!databaseconnection::Open())
    {
        QMessageBox::about(this,"Message","connect error");
        return;
    }
    else
    {
        if(floor!="")
        {
            flg=true;
            cmd = cmd+"floor='"+floor+"' ";
            //QMessageBox::about(this,"message",cmd);
        }
         if(type!="")
        {
            // cmd = "select * from house.roomInfo where ";
             if(flg==true)
                cmd = cmd+"and type like '%"+type+"%' ";
             else
             {
                 cmd = cmd+"type like '%"+type+"%' ";
                 flg=true;
             }
        }
       if(areamin!="")
        {
           if(flg==true)
            cmd = cmd+"and area>='"+areamin+"' ";            //QMessageBox::about(this,"message",cmd);
            else
           {
                cmd = cmd+"area>='"+areamin+"' ";
                flg=true;
            }
        }
         if(areamax!="")
        {
             if(flg==true)
                cmd = cmd+ "and area<='"+areamax+"' ";
              else
             {
                  cmd = cmd+ "area<='"+areamax+"' ";
                  flg=true;
              }
        }
         if(pricemin!="")
        {
             if(flg==true)
                cmd = cmd+"and price>='"+pricemin+"' ";
            else
             {
                cmd = cmd+"price>='"+pricemin+"' ";
                flg=true;
            }
        }
         if(pricemax!="")
        {
             if(flg==true)
                cmd = cmd+"and price<='"+pricemax+"' ";
            else
             {
                 cmd = cmd+"price<='"+pricemax+"' ";
                 flg=true;
             }
        }
         if(location!="")
        {
             if(flg==true)
             {
                 cmd = cmd+"and location like '%"+location+"%' ";

              }
             else
             {
                  cmd = cmd+"location like '%"+location+"%' ";
                  flg=true;
             }
        }
        if(cmd==cmdTmp)
        {
            cmd = "select * from house.roomInfo";

        }
        QSqlQueryModel *model = new QSqlQueryModel;
        model->setQuery(cmd);
        model->setHeaderData(0,Qt::Horizontal,tr("roomId"));
        model->setHeaderData(1,Qt::Horizontal,tr("renterid"));
        model->setHeaderData(2,Qt::Horizontal,tr("roomtype"));
        model->setHeaderData(3,Qt::Horizontal,tr("location"));
        model->setHeaderData(4,Qt::Horizontal,tr("floor"));
        model->setHeaderData(5,Qt::Horizontal,tr("ratingNum"));
        model->setHeaderData(6,Qt::Horizontal,tr("realNum"));
        model->setHeaderData(7,Qt::Horizontal,tr("area"));
        model->setHeaderData(8,Qt::Horizontal,tr("price"));
        model->setHeaderData(9,Qt::Horizontal,tr("air"));
        model->setHeaderData(10,Qt::Horizontal,tr("phone"));
        model->setHeaderData(11,Qt::Horizontal,tr("tv"));
        model->setHeaderData(12,Qt::Horizontal,tr("washer"));
        model->setHeaderData(13,Qt::Horizontal,tr("internet"));
        model->setHeaderData(14,Qt::Horizontal,tr("fridge"));
        model->setHeaderData(15,Qt::Horizontal,tr("remark"));
        ui->tbvSearchHouse->setModel(model);

        ui->tbvSearchHouse->show();
    }
    databaseconnection::Close();
}

void searchhouse::on_btnEmpty_clicked()
{
    if(!databaseconnection::Open())
    {
        QMessageBox::about(this,"Message","connect error");
        return;
    }
    else
    {
        QSqlQueryModel *model = new QSqlQueryModel;
        model->setQuery("select * from house.roomInfo where realNum='0'");
        model->setHeaderData(0,Qt::Horizontal,tr("roomId"));
        model->setHeaderData(1,Qt::Horizontal,tr("renterid"));
        model->setHeaderData(2,Qt::Horizontal,tr("roomtype"));
        model->setHeaderData(3,Qt::Horizontal,tr("location"));
        model->setHeaderData(4,Qt::Horizontal,tr("floor"));
        model->setHeaderData(5,Qt::Horizontal,tr("ratingNum"));
        model->setHeaderData(6,Qt::Horizontal,tr("realNum"));
        model->setHeaderData(7,Qt::Horizontal,tr("area"));
        model->setHeaderData(8,Qt::Horizontal,tr("price"));
        model->setHeaderData(9,Qt::Horizontal,tr("air"));
        model->setHeaderData(10,Qt::Horizontal,tr("phone"));
        model->setHeaderData(11,Qt::Horizontal,tr("tv"));
        model->setHeaderData(12,Qt::Horizontal,tr("washer"));
        model->setHeaderData(13,Qt::Horizontal,tr("internet"));
        model->setHeaderData(14,Qt::Horizontal,tr("fridge"));
        model->setHeaderData(15,Qt::Horizontal,tr("remark"));
        ui->tbvSearchHouse->setModel(model);
        ui->tbvSearchHouse->show();
    }
    databaseconnection::Close();
}

void searchhouse::on_btnCancel_clicked()
{
    this->close();
}
